<template>
  <div class="app-container">
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="标题" prop="title" style="width: 400px">
            <el-input v-model="form.title" placeholder="请输入标题" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="分类" prop="type" style="width: 400px">
            <el-cascader
              v-model="form.sceneType"
              placeholder="请选择分类"
              :options="placeTypeOption"
              :props="{value:'id',label:'data',multiple: true}"
              filterable></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="举办时间" prop="title" style="width: 400px">
            <el-date-picker
              v-model="time"
              value-format="yyyy-MM-dd HH:mm"
              size="small"
              clearable
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              align="left"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主办单位" prop="title" style="width: 400px">
            <el-input v-model="form.organizer" placeholder="请输入主办单位" clearable  />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">

        <el-col :span="24">
          <el-form-item label="承办单位" prop="title" style="width: 400px">
            <el-input v-model="form.organizerTake" placeholder="请输入承办单位" clearable  />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="协办单位" prop="title" style="width: 400px">
            <el-input v-model="form.organizerCo" placeholder="请输入协办单位" clearable  />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="封面图片" prop="Abstract" style="width: 800px">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.coverUrl" :src="form.coverUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="form.coverUrl" alt="">
        </el-dialog>
      </el-row>


      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="介绍" prop="Abstract">
            <div class="container">
              <Toolbar :editor="editor" :default-config="toolbarConfig" :mode="mode" />
              <Editor v-model="form.eventIntro" :default-config="editorConfig" :mode="mode" @onCreated="onCreated" />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {bakGetJobFairByIdApi,insertAndUpdateJobFairApi} from "@/api/jobFair";
export default {
  components: { Editor, Toolbar },
  name: 'PlaceCodeDetail',
  data(){
    return{
      editor: null,
      toolbarConfig: {
        excludeKeys: [
          // 排除菜单组，写菜单组 key 的值即可
          'group-video' // 去掉视频
        ]
      },
      editorConfig: { MENU_CONF: {}, placeholder: '' },
      mode: 'default',
      uploadUrl: process.env.VUE_APP_UPLOAD_API,
      dialogImageUrl: '',
      dialogVisible: false,
      time:[],
      form: {
        id: null,
        title: '',
        eventTimeStart:'',
        eventTimeEnd:'',
        organizer: '',
        organizerTake:'',
        organizerCo:'',
        eventIntro:'',
        coverUrl:'',
        sceneType:'',
      },
      rules: {
        title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        company: [{ required: true, trigger: 'blur', message: '请输入培训机构' }],
        time: [{ required: true, trigger: 'blur', message: '请选择时间' }],
        organizer: [{ required: true, trigger: 'blur', message: '请输入主办单位' }],
        organizerTake: [{ required: true, trigger: 'blur', message: '请输入承办单位' }],
        organizerCo: [{ required: true, trigger: 'blur', message: '请输入协办单位' }],
        coverUrl: [{ required: true, trigger: 'blur', message: '请上传封面图片' }],
        sceneType: [{ required: true, trigger: 'blur', message: '请选择分类' }],
        eventIntro: [{ required: true, trigger: 'blur', message: '请输入介绍' }],
      },
      formLoading: false,
      placeTypeLoading:true,
      placeTypeOption:[
        {id:1,data: '现场招聘'},
        {id:2,data: '网络招聘'},
        {id:3,data: '校园招聘'},
      ],
    }
  },
  props:['id','closeDialog'],
  methods:{
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.form.coverUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },

    submit(){
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading=this.$loading({
            text:'提交中'
          })
          if(this.form.id==null)
          {
            this.form.id = 0;
          }

          if(this.time!=null)
          {
            this.form.eventTimeStart = this.time[0];
            this.form.eventTimeEnd = this.time[1];
          }
          const form=JSON.parse(JSON.stringify(this.form));
          console.log(this.form.sceneType)
          if(typeof this.form.sceneType!=='number'){
            form.sceneType=form.sceneType.length>0?form.sceneType.join(','):'';
          }
          insertAndUpdateJobFairApi(form).then((res)=>{
            this.$message.success('保存成功');
            this.closeDialog(0);
          }).catch(()=>{
            this.$message.error('保存失败');
          }).finally(()=>{
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getDetail(){
      this.formLoading=true;
      bakGetJobFairByIdApi({id:this.id}).then((res)=>{
        this.form=res;
        this.$set(this.time,0,res.eventTimeStart)
        this.$set(this.time,1,res.eventTimeEnd)
        this.form.sceneType=this.form.sceneType.split(',');
      }).finally(()=>{
        this.formLoading=false;
      })
    },
  },
  mounted() {
    if(this.id){
      this.getDetail();
    }
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  created() {
    this.editorConfig.placeholder = '请输入使用说明内容...'
    this.editorConfig.MENU_CONF['uploadImage'] = {
      timeout: 5 * 1000, // 5s
      fieldName: 'image',
      // 用户自定义上传图片
      customUpload(file, insertFn) {
        var axios = require('axios')
        var FormData = require('form-data')
        var data = new FormData()
        data.append('file', file) // file 即选中的文件
        var config = {
          method: 'post',
          url: process.env.VUE_APP_UPLOAD_API, // 上传图片地址
          data: data
        }
        axios(config)
          .then(function(res) {
            const url = res.data.imgURL // 拼接成可浏览的图片地址
            insertFn(url, '使用说明', url) // 插入图片
          })
          .catch((res) => {
            console.log(res)
          })
      }
    }
  },
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
</style>
<style scoped>
/*
*让select撑开
*/
.el-select,.el-cascader{
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
